/// <reference path="typings/tsd.d.ts" />
var PlayAudio = (function () {
    function PlayAudio(element) {
        this.el = element;
        this.audios = [];
    }
    PlayAudio.prototype.putAudios = function (element) {
        var _this = this;
        var srcs = element.getAttribute('data-src');
        srcs.split('|').forEach(function (audio) {
            _this.audios.push(audio);
        });
    };
    PlayAudio.prototype.choiceAudioType = function (src) {
        if (src.match(/\.(mp3)$/)) {
            return 'audio/mpeg';
        }
        if (src.match(/\.(ogg)$/)) {
            return 'audio/ogg';
        }
        return 'audio/mpeg';
    };
    PlayAudio.prototype.createAudioEl = function () {
        var _this = this;
        var audioEl = document.createElement('audio');
        this.audios.forEach(function (audio) {
            var sourceEl = document.createElement('source');
            sourceEl.setAttribute('src', audio);
            sourceEl.setAttribute('type', _this.choiceAudioType(audio));
            audioEl.appendChild(sourceEl);
        });
        return audioEl;
    };
    PlayAudio.prototype.render = function (wrapper) {
        this.putAudios(this.el);
        var audio = this.createAudioEl();
        wrapper.appendChild(audio);
        this.el.addEventListener('click', function () {
            audio.load();
            audio.currentTime = 0;
            audio.play();
        });
    };
    return PlayAudio;
})();
document.addEventListener('DOMContentLoaded', function () {
    jQuery('body').find('[data-action="play.audio"]').each(function (i, el) {
        var playAudio = new PlayAudio(el);
        playAudio.render(document.getElementsByTagName('body')[0]);
    });
});
